<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avarter_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单 -->
      <el-form
        label-width="0px"
        class="login_from"
        :model="loginfrom"
        :rules="loginrules"
        ref="FromLogin"
      >
        <!-- 用户名 -->
        <el-form-item prop="usename">
          <el-input
            prefix-icon="el-icon-user"
            placeholder="用户名"
            v-model="loginfrom.usename"
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="usepwd">
          <el-input
            prefix-icon="el-icon-unlock"
            placeholder="密码"
            v-model="loginfrom.usepwd"
            type="password"
          ></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item class="butns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="danger" @click="resetFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginfrom: {
        usename: '',
        usepwd: ''
      },
      // 表单规则验证
      loginrules: {
        usename: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // console.log(111),
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'change' }
        ],
        usepwd: [
          // console.log(112),
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    resetFrom: function () {
      console.log(this)
      this.$refs.FromLogin.resetFields()
      // console.log(this)
    },
    login: function () {
      this.$refs.FromLogin.validate(async valid => {
        if (valid) {
          // console.log(valid)
          const resuil = await this.$http.post('login', this.loginfrom)
          // console.log(this.loginfrom)
          // console.log(resuil)
          if (resuil.data.message === true) {
            console.log('登录成功')
            console.log(resuil)
            window.sessionStorage.setItem('cookie', resuil.data.reqs.cookie)
            this.$router.push('/home')
          } else {
            console.log('登录失败')
            return this.$message.error('账号或密码错误')
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  position: absolute;
  width: 450px;
  height: 300px;
  background-color: #fff;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avarter_box img {
  height: 130px;
  width: 130px;
  border: 1px solid #eeeeee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px pink;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    height: 100%;
    width: 100%;
    border-radius: 50%;
    background-color: darkgrey;
  }
}

.butns {
  display: flex;
  justify-content: flex-end;
}
.login_from {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
